package com.sloan.community.controller;

import com.sloan.community.common.query.SearchQuery;
import com.sloan.community.common.vo.IndexArticleVO;
import com.sloan.community.common.vo.IndexHotVO;
import com.sloan.community.common.vo.PageVo;
import com.sloan.community.service.IndexService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import reactor.core.publisher.Mono;

import java.util.List;

import static com.sloan.community.common.Constant.STYLES;

@Slf4j
@Controller
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class IndexController {

    private final IndexService indexService;

    @GetMapping({"/", "index", "index.html"})
    public Mono<String> index(SearchQuery searchQuery, Model model) {
        Mono<PageVo<IndexArticleVO>> articleMono = indexService.articlePageList(searchQuery);
        Mono<List<IndexHotVO>> hotList=indexService.hotList();
        return Mono.zip(articleMono, hotList)
                .map(item -> {
                    model.addAttribute("keyword", searchQuery.getKeyword());
                    model.addAttribute("articleList", item.getT1());
                    model.addAttribute("hotList",item.getT2());
                    model.addAttribute("styles", STYLES);
                    return "index";
                });
    }
}
